local game = require 'club.threecard.texas'

local ingame = 
{
    throttleable = true,
}

local phases = 
{
    none     = {id = 0, name = 'None'},
    idle     = {id = 10, name = '空闲'},
    ready    = {id = 20, name = '准备'},
    button   = {id = 30, name = '定庄'},
    perflop  = {id = 40, name = '盲注'},
    flop     = {id = 50, name = '二轮'},
    turn     = {id = 60, name = '三轮'},
    river    = {id = 70, name = '四轮'},
    showdown = {id = 80, name = '摊牌'},
    settle   = {id = 90, name = '结算'},
}

local phases_by_id = 
{
    [phases.none.id] = phases.none,
    [phases.idle.id] = phases.idle,
    [phases.ready.id] = phases.ready,
    [phases.button.id] = phases.button,
    [phases.perflop.id] = phases.perflop,
    [phases.flop.id] = phases.flop,
    [phases.turn.id] = phases.turn,
    [phases.river.id] = phases.river,
    [phases.showdown.id] = phases.showdown,
    [phases.settle.id] = phases.settle,
}

ingame.state = 
{
    phases = phases,

    phase_id = phases.none.id,
    current_bonus = 0,

    button_no = 0,
    s_blind_no = 0,
    l_blind_no = 0,
    s_blind_value = 0,
    l_blind_value = 0,

    self_card_ids = {},
    self_handtype = 0,

    public_card_ids = {},
    left_time = 0,

    current_no = 0,
    current_operation_ids,
    current_operation_values,

    showdown_info = {},
}

ingame.mutations = 
{   
    on_current_bonud = function(store, state, current_bonus)
        state.current_bonus = current_bonus
        store.emit('奖金池更新')
    end,

    on_set_chip = function(store, state, args)
        store.emit("筹码变更", args)
    end,

    on_phase = function(store, state, args)
        local phase_id = args.phase_id
        local countdown = args.countdown
        state.phase_id = args.phase_id
        game.gamelib.log.debug('阶段变更：'..tostring(phases_by_id[state.phase_id].name))
        store.emit("阶段变更");
    end,

    on_deal = function(store, state, args)
        state.self_card_ids = args.card_ids
        local action_sequence = args.action_sequence
        store.emit('发牌', action_sequence)
    end,

    on_sync_handtype = function(store, state, handtype)
        state.self_handtype = handtype
        store.emit('同步手牌牌型')
    end,

    on_notice_button = function(store, state, args)
        state.button_no = args.button_no
        state.s_blind_no = args.s_blind_no
        state.l_blind_no = args.l_blind_no

        store.emit('定庄')
    end,

    on_notice_blind = function(store, state, args)
        state.s_blind_value = args.s_blind_value
        state.l_blind_value = args.l_blind_value

        store.emit('下盲注')
    end,

    on_notice_burn = function(store, state, count)
        store.emit('销牌', count)
    end,

    on_notice_deal = function(store, state, card_ids)
        for _, card_id in ipairs(card_ids) do
            table.insert(state.public_card_ids, card_id)
        end
        store.emit('公牌', card_ids)
    end,

    on_turn_ope = function(store, state, args)
        state.current_no = args.no
        state.current_operation_ids = args.operation_ids
        state.current_operation_values = args.operation_values
        state.left_time = args.time or 5000
        state.left_time = state.left_time/1000
        store.emit('请求玩家操作')
    end,

    on_player_ope = function(store, state, args)
        local no = args.no
        local operation_id = args.operation_id
        local value = args.value
        store.emit('玩家操作', {no = no, operation_id = operation_id, value = value})
    end,

    on_showdown = function(store, state, showdown_info)
        state.showdown_info = showdown_info
        store.emit('摊牌')
    end,

    on_settle = function(store, state, settle_info)
        store.emit('结算', settle_info)
    end,

    on_game_init = function(store, state, scene)
        state.scene = scene
        state.button_no = scene.button_no
        state.s_blind_no = scene.s_blind_no
        state.l_blind_no = scene.l_blind_no
        state.public_card_ids = scene.public_card_ids
        state.operation_ids = scene.operation_ids
        state.operation_values = scene.operation_values
        state.current_no = scene.current_no
        state.left_time = scene.left_time
        state.self_handtype = scene.self_player.hand_type
        state.self_card_ids = scene.self_player.card_ids
        game.coroutine.ingame_stop_all()
        store.emit('恢复游戏场景')
    end
}

return ingame